<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="数据年份" prop="year">
        <el-input v-model="queryParams.year" placeholder="请输入数据年份" clearable style="width: 240px"
          @keyup.enter="handleQuery" />
      </el-form-item>
      <el-form-item label="主体名称" prop="planterName">
        <el-input v-model="queryParams.planterName" placeholder="请输入主体名称" clearable style="width: 240px"
          @keyup.enter="handleQuery" />
      </el-form-item>
      <el-form-item label="主体类型" prop="planterType">
        <el-select v-model="queryParams.planterType" placeholder="请选择主体类型" clearable style="width: 240px">
          <el-option v-for="dict in planter_type" :key="dict.value" :label="dict.label" :value="dict.value" />
        </el-select>
      </el-form-item>
      <el-form-item label="所在县" prop="county">
        <el-input v-model="queryParams.county" placeholder="请输入所在县名称" clearable style="width: 240px"
          @keyup.enter="handleQuery" />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="Plus" @click="handleAdd" v-hasPermi="['plant:planter:add']">新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="success" plain icon="Edit" :disabled="single" @click="handleUpdate"
          v-hasPermi="['plant:planter:edit']">修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="danger" plain icon="Delete" :disabled="multiple" @click="handleDelete"
          v-hasPermi="['plant:planter:remove']">删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="warning" plain icon="Download" @click="handleExport"
          v-hasPermi="['plant:planter:export']">导出</el-button>
      </el-col>
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="planterList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="种植主体ID" align="center" prop="planterId" />
      <el-table-column label="数据年份" align="center" prop="year" />
      <el-table-column label="主体名称" align="center" prop="planterName" />
      <el-table-column label="主体类型" align="center" prop="planterType">
        <template #default="scope">
          <dict-tag :options="planter_type" :value="scope.row.planterType" />
        </template>
      </el-table-column>
      <el-table-column label="所在县" align="center" prop="county" />
      <el-table-column label="托管服务面积" align="center" prop="areaSizeService" />
      <el-table-column label="种植主体负责人" align="center" prop="principal" />
      <el-table-column label="负责人联系电话" align="center" prop="principalMobile" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template #default="scope">
          <el-button link type="primary" icon="Edit" @click="handleUpdate(scope.row)"
            v-hasPermi="['plant:planter:edit']">修改</el-button>
          <el-button link type="primary" icon="Delete" @click="handleDelete(scope.row)"
            v-hasPermi="['plant:planter:remove']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total > 0" :total="total" v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize" @pagination="getList" />

    <!-- 添加或修改种植主体信息对话框 -->
    <el-dialog :title="title" v-model="open" width="80%" append-to-body>
      <el-form ref="planterRef" :model="form" :rules="rules" label-width="120px">
        <el-row>
          <el-col :span="12">
            <el-form-item label="主体名称" prop="planterName">
              <el-input v-model="form.planterName" placeholder="请输入主体名称" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="主体类型" prop="planterType">
              <el-select v-model="form.planterType" placeholder="请选择主体类型">
                <el-option v-for="dict in planter_type" :key="dict.value" :label="dict.label"
                  :value="dict.value"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="主体负责人" prop="principal">
              <el-input v-model="form.principal" placeholder="请输入主体负责人姓名" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="联系电话" prop="principalMobile">
              <el-input v-model="form.principalMobile" placeholder="请输入负责人联系电话" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="身份证号" prop="principalIdno">
              <el-input v-model="form.principalIdno" placeholder="请输入负责人身份证号" />
            </el-form-item></el-col><el-col :span="12">
            <el-form-item v-if="form.planterType == 1 | form.planterType == 3" label="纳税人识别号" prop="taxerNo">
              <el-input v-model="form.taxerNo" placeholder="请输入纳税人识别号" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="所在县" prop="county">
              <el-input v-model="form.county" placeholder="请输入所在县" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="所在村" prop="village">
              <el-input v-model="form.village" placeholder="请输入所在村" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="签约状态" prop="signState">
              <el-radio-group v-model="form.signState">
                <el-radio v-for="dict in biz_yes_no" :key="dict.value" :label="dict.value">{{ dict.label }} </el-radio>
              </el-radio-group>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item v-if="form.signState == 1" label="村集体流转面积" prop="areaSizePolicy">
              <el-input v-model="form.areaSizePolicy" placeholder="请输入村集体流转面积"><template #append>亩</template></el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item v-if="form.signState == 1" label="量地状态" prop="measureState">
              <el-radio-group v-model="form.measureState">
                <el-radio v-for="dict in biz_yes_no" :key="dict.value" :label="dict.value">{{ dict.label }} </el-radio>
              </el-radio-group>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item v-if="form.measureState == 1" label="实际量地面积" prop="areaSizeMeasured">
              <el-input v-model="form.areaSizeMeasured" placeholder="请输入实际量地面积"><template
                  #append>亩</template></el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item v-if="form.signState == 1" label="设施面积" prop="areaSizeFacility">
              <el-input v-model="form.areaSizeFacility" placeholder="请输入签约设施种植面积"><template
                  #append>亩</template></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item v-if="form.signState == 1" label="设施棚数" prop="areaCountFacility">
              <el-input v-model="form.areaCountFacility" placeholder="请输入签约设施种植棚数"><template
                  #append>个</template></el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item v-if="form.signState == 1" label="露地面积" prop="areaSizeOpenair">
              <el-input v-model="form.areaSizeOpenair" placeholder="请输入露地签约面积"><template #append>亩</template></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item v-if="form.signState == 1" label="托管服务面积" prop="areaSizeService">
              <el-input v-model="form.areaSizeService" placeholder="请输入托管服务面积"><template #append>亩</template></el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item v-if="form.signState == 1" label="是否需付地租" prop="isRent">
              <el-radio-group v-model="form.isRent">
                <el-radio v-for="dict in biz_yes_no" :key="dict.value" :label="dict.value">{{ dict.label }} </el-radio>
              </el-radio-group>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item v-if="form.signState == 1" label="是否需付种苗款" prop="isSeed">
              <el-radio-group v-model="form.isSeed">
                <el-radio v-for="dict in biz_yes_no" :key="dict.value" :label="dict.value">{{ dict.label }} </el-radio>
              </el-radio-group>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item v-if="form.signState == 1 & form.areaSizeFacility > 0" label="设施种植密度"
              prop="plantDensityFacility">
              <el-input v-model="form.plantDensityFacility" placeholder="请输入设施种植密度"
                @change="calulateSeedAmount"><template #append>棵/亩</template></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item v-if="form.signState == 1 & form.areaSizeOpenair > 0" label="露地种植密度"
              prop="plantDensityOpenair">
              <el-input v-model="form.plantDensityOpenair" placeholder="请输入露地种植密度"
                @change="calulateSeedAmount"><template #append>棵/亩</template></el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item v-if="form.signState == 1" label="预估种苗数量" prop="seedAmountEstimated">
              <el-input v-model="form.seedAmountEstimated" placeholder="请输入预估种苗数量"><template
                  #append>棵</template></el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item v-if="form.signState == 1" label="银行开户名称" prop="bankAccountName">
              <el-input v-model="form.bankAccountName" placeholder="请输入内容" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item v-if="form.signState == 1" label="银行账号" prop="bankAccount">
              <el-input v-model="form.bankAccount" placeholder="请输入银行账号" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item v-if="form.signState == 1" label="开户银行" prop="bankName">
              <el-input v-model="form.bankName" placeholder="请输入开户银行" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="备注" prop="remark">
              <el-input v-model="form.remark" type="textarea" placeholder="请输入内容" />
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="Planter">
import { listPlanter, getPlanter, delPlanter, addPlanter, updatePlanter } from "@/api/base/planter";
import { getTime } from "element-plus/es/components/countdown/src/utils.mjs";

const { proxy } = getCurrentInstance();
const { planter_type, biz_yes_no } = proxy.useDict('planter_type', 'biz_yes_no');

const planterList = ref([]);
const open = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const title = ref("");

const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    year: null,
    planterName: null,
    planterType: null,
    county: null
  },
  rules: {
  }
});

const { queryParams, form, rules } = toRefs(data);

/** 查询种植主体信息列表 */
function getList() {
  loading.value = true;
  listPlanter(queryParams.value).then(response => {
    planterList.value = response.rows;
    total.value = response.total;
    loading.value = false;
  });
}

/** 取消按钮 */
function cancel() {
  open.value = false;
  reset();
}

/** 表单重置 */
function reset() {
  form.value = {
    planterId: null,
    year: null,
    planterName: null,
    planterType: null,
    areaSizeMeasured: null,
    county: null,
    village: null,
    areaSizeService: null,
    measureState: null,
    signState: null,
    isRent: null,
    principal: null,
    areaSizeFacility: null,
    principalMobile: null,
    principalIdno: null,
    isSeed: null,
    seedAmountEstimated: null,
    areaSizeOpenair: null,
    taxerNo: null,
    bankAccountName: null,
    remark: null,
    areaSizePolicy: null,
    plantDensityOpenair: null,
    plantDensityFacility: null,
    bankAccount: null,
    bankName: null,
  };
  proxy.resetForm("planterRef");
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef");
  handleQuery();
}

/** 多选框选中数据  */
function handleSelectionChange(selection) {
  ids.value = selection.map(item => item.planterId);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
}

/** 新增按钮操作 */
function handleAdd() {
  reset();
  open.value = true;
  title.value = "添加种植主体信息";
}

/** 修改按钮操作 */
function handleUpdate(row) {
  reset();
  const _planterId = row.planterId || ids.value;
  getPlanter(_planterId).then(response => {
    form.value = response.data;
    open.value = true;
    title.value = "修改种植主体信息";
  });
}

/** 提交按钮 */
function submitForm() {
  proxy.$refs["planterRef"].validate(valid => {
    if (valid) {
      if (form.value.planterId != null) {
        updatePlanter(form.value).then(response => {
          proxy.$modal.msgSuccess("修改成功");
          open.value = false;
          getList();
        });
      } else {
        addPlanter(form.value).then(response => {
          proxy.$modal.msgSuccess("新增成功");
          open.value = false;
          getList();
        });
      }
    }
  });
}

/** 删除按钮操作 */
function handleDelete(row) {
  const _planterIds = row.planterId || ids.value;
  proxy.$modal.confirm('是否确认删除种植主体信息编号为"' + _planterIds + '"的数据项？').then(function () {
    return delPlanter(_planterIds);
  }).then(() => {
    getList();
    proxy.$modal.msgSuccess("删除成功");
  }).catch(() => { });
}


/** 导出按钮操作 */
function handleExport() {
  proxy.download('plant/planter/export', {
    ...queryParams.value
  }, `planter_${new Date().getTime()}.xlsx`);
}

/** 是否渲染字段 */
function renderField(insert, edit) {
  return form.value.planterId == null ? insert : edit;
}

function calulateSeedAmount() {
  let seedAmountEstimated = 0

  form.seedAmountEstimated = seedAmountEstimated
}

getList();
</script>